{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import GsSession, Environment\n",
    "from gs_quant.instrument import EqOption, OptionType, OptionStyle\n",
    "from gs_quant.backtests.strategy import Strategy\n",
    "from gs_quant.backtests.triggers import *\n",
    "from gs_quant.backtests.actions import *\n",
    "from gs_quant.backtests.equity_vol_engine import *\n",
    "from gs_quant.target.common import UnderlierType\n",
    "from datetime import date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Define backtest dates\n",
    "start_date = date(2019, 9, 4)\n",
    "end_date = date(2020, 9, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Define option instrument for strategy\n",
    "option = EqOption(\n",
    "    'SX5E',\n",
    "    underlierType=UnderlierType.BBID,\n",
    "    expirationDate='3m',\n",
    "    strikePrice='ATM',\n",
    "    optionType=OptionType.Call,\n",
    "    optionStyle=OptionStyle.European,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# Define a periodic trade trigger and action.  Trade and roll 1000 units of the option instrument every 1m\n",
    "trade_action = EnterPositionQuantityScaledAction(\n",
    "    priceables=option,\n",
    "    trade_duration='1m',\n",
    "    trade_quantity=1000,\n",
    "    trade_quantity_type=BacktestTradingQuantityType.quantity,\n",
    ")\n",
    "trade_trigger = PeriodicTrigger(\n",
    "    trigger_requirements=PeriodicTriggerRequirements(start_date=start_date, end_date=end_date, frequency='1m'),\n",
    "    actions=trade_action,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create strategy\n",
    "strategy = Strategy(initial_portfolio=None, triggers=[trade_trigger])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run backtest\n",
    "backtest = EquityVolEngine.run_backtest(strategy, start=start_date, end=end_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot the performance\n",
    "pnl = backtest.get_measure_series(FlowVolBacktestMeasure.PNL)\n",
    "pnl.plot(legend=True, label='PNL Total')\n",
    "\n",
    "pnl_carry = backtest.get_measure_series(FlowVolBacktestMeasure.PNL_carry)\n",
    "pnl_carry.plot(legend=True, label='PNL Carry')\n",
    "\n",
    "pnl_vol = backtest.get_measure_series(FlowVolBacktestMeasure.PNL_vol)\n",
    "pnl_vol.plot(legend=True, label='PNL Vol')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
